package org.jeecg.modules.third.jg.jss.report.cfccapitalflow.service;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.jg.jss.report.cfccapitalflow.entity.CfcCapitalflow;
import org.jeecg.modules.third.jg.jss.report.cfccapitalflow.mapper.CfcCapitalflowMapper;
import org.jeecg.modules.third.jg.jss.report.cfcjoborderinfo.entity.CfcJoborderinfo;
import org.jeecg.modules.third.jg.jss.report.cfcjoborderinfo.service.CfcJoborderinfoService;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 资金流水信息
 * @Description: CFC_CAPITALFLOW
 * @Author: jeecg-boot
 * @Date:   2021-11-25
 * @Version: V1.0
 */
@Service
@Slf4j
public class CfcCapitalflowService {

    @Resource
    private CfcCapitalflowMapper capitalflowMapper;

    @Resource
    private CfcJoborderinfoService cfcJoborderinfoService;

    public String getEnterPriseCode() {
        return enterPriseCode;
    }

    public void setEnterPriseCode(String enterPriseCode) {
        this.enterPriseCode = enterPriseCode;
    }

    private String enterPriseCode;

    public  boolean inserCapitalflow(List<HyShippingNoteBill> shippingNoteBills , HyShippingNote hyShippingNote){
        if (CollectionUtils.isEmpty(shippingNoteBills)){
            throw new JeecgBootException("当前运单支付明细不存在.");
        }

        HyShippingNoteBill shippingNoteBill = shippingNoteBills.get(0);

        CfcCapitalflow cfcCapitalflow = capitalflowMapper.selectCapitalflow(shippingNoteBill.getId());
        if(cfcCapitalflow != null){
            log.warn("该资金流水单已存在, ShippingId : {} ", shippingNoteBill.getId());
            throw new JeecgBootException("资金流水单数据重复上报.");
        }

        CfcJoborderinfo cfcJoborderinfo = cfcJoborderinfoService.getJoborderinfoByShinpNum(hyShippingNote.getShippingNoteNumber());

        cfcCapitalflow = getCfcCapitalflow(shippingNoteBill, hyShippingNote, cfcJoborderinfo);
        //log.info("江苏资金流水单数据, {}", JSONUtil.toJsonStr(cfcCapitalflow));
        Integer res = capitalflowMapper.inserCapitalflow(cfcCapitalflow);
        return res > 0 ? true : false;
    }


    public CfcCapitalflow getCfcCapitalflow(HyShippingNoteBill shippingNoteBill, HyShippingNote hyShippingNote,
                                             CfcJoborderinfo cfcJoborderinfo) {
        if(shippingNoteBill==null || hyShippingNote == null ||cfcJoborderinfo == null){
            return null;
        }

        CfcCapitalflow cfcCapitalflow = new CfcCapitalflow();
        cfcCapitalflow.setPlatformcode(this.getEnterPriseCode());

        cfcCapitalflow.setDocumentnumber(shippingNoteBill.getId());
        cfcCapitalflow.setSerialnumber(shippingNoteBill.getBillNum());
        cfcCapitalflow.setPartfunctioncode("CA");
        cfcCapitalflow.setPartname(shippingNoteBill.getCarrier());
        cfcCapitalflow.setCarrierid(shippingNoteBill.getDriverId()) ;
        cfcCapitalflow.setDatetime(hyShippingNote.getPayDt());

        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;

        cfcCapitalflow.setMonetaryamount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN)
        );
        cfcCapitalflow.setPaymentmeans(StringUtils.isNotEmpty(shippingNoteBill.getPaymentMeansCode()) ? shippingNoteBill.getPaymentMeansCode()  : "32");
        cfcCapitalflow.setJobordernumber( cfcJoborderinfo.getJobordernumber());
        cfcCapitalflow.setBankcode(shippingNoteBill.getBankCode());

        cfcCapitalflow.setTotalmonetaryamount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN)
        );

        cfcCapitalflow.setSerialnum("0000");
        cfcCapitalflow.setReceipt(shippingNoteBill.getCarrier());
        cfcCapitalflow.setReceiptaccount(
                "1".equals(shippingNoteBill.getBillImportFlag()) ?
                        shippingNoteBill.getCraccno() :
                        shippingNoteBill.getCraccno()+shippingNoteBill.getCrlogaccno()
        );
        cfcCapitalflow.setRecbankcode(shippingNoteBill.getBankCode());
        return cfcCapitalflow;
    }



    public CfcCapitalflow selectCapitalflow(String documentnumber) {
        return capitalflowMapper.selectCapitalflow( documentnumber );
    }


    public Integer deletCapitalflow(String documentnumber) {
        return capitalflowMapper.deletCapitalflow( documentnumber );
    }


}
